class Main extends egret.DisplayObjectContainer {

    private loadingView:LoadingUI;
    private miner:egret.Bitmap;  //矿工
    private line;  //钩子和线
    private timeContainer;  //时间
    private hourglass;   //沙漏
    private chances:Chances;   //机会
    private nochance:NoChancesTips;
    private TimeClass:Time;      //时间Time类
    private time:number = 30;   //倒计时30s
    private stageW:number;   //舞台宽度
    private stageH:number;   //舞台高度
    private score:Score;     //分数Score类
    private tmp: Array<egret.Bitmap> = [];   //存放金子、石头等元素的数组
    private s:number;       //作为每次传参的分数
    private coordinate:Array<any> = [];     //金子、石头等元素摆放的格子元素，类似棋盘
    private v_coordinate:number;           //coordinate格子竖坐标
    private gameStartTime:Gamestart;   //游戏开始
    private startTime:number = 3;   //游戏开始前倒数3s
    private timer:egret.Timer;      //
    private gameOver:Gameover;  //游戏结束的类
    private _timer;  //定时器
    private index:number = 2; //矿工对应状态
    private request:Request;  //http请求
    public respone = new Object;
    private moveTime:number = 3;  //规则tips停留时间
    // private tipsBG:egret.Bitmap;  //规则tips背景图
    private tipsBG:Reqular;  //规则类
    private shareTips:Share;  //分享类
    private failload:FailLoad;  //加载失败类

    public constructor() {
        super();       
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event:egret.Event) {
        //设置加载进度界面
        //Config to load process interface
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);

        //先加载loadding图片
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onLoadingComplete, this);
        RES.loadConfig("loadingResource/default.res.json", "loadingResource/");
      
    }

    private onLoadingComplete(event:RES.ResourceEvent):void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onLoadingComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadingComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadingError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceloadingProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadingError, this);
        RES.loadGroup("preloading");
    }

    private onResourceLoadingComplete(event:RES.ResourceEvent):void {
        if (event.groupName == "preloading") {
            // this.stage.removeChild(this.loadingView);
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadingComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadingError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceloadingProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadingError, this);

            //初始化Resource资源加载库
            //initiate Resource loading library
            RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
            RES.loadConfig("resource/default.res.json", "resource/");
        }
    }

    /**
     * 资源组加载出错
     *  The resourceloading group loading failed
     */
    private onItemLoadingError(event:RES.ResourceEvent):void {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }

    /**
     * 资源组加载出错
     *  The resourceloading group loading failed
     */
    private onResourceLoadingError(event:RES.ResourceEvent):void {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //Ignore the loading failed projects
        this.onResourceLoadComplete(event);
    }

    /**
     * preloading资源组加载进度
     * Loading process of preloading resource group
     */
    private onResourceloadingProgress(event:RES.ResourceEvent):void {
        if (event.groupName == "preloading") {                   
            // this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }


    // -----------------分割线-----------------------

    /**
     * 配置文件加载完成,开始预加载preload资源组。
     * configuration file loading is completed, start to pre-load the preload resource group
     */
    private onConfigComplete(event:RES.ResourceEvent):void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup("preload");
    }

    /**
     * preload资源组加载完成
     * Preload resource group is loaded
     */
    private onResourceLoadComplete(event:RES.ResourceEvent):void {
        if (event.groupName == "preload") {
            this.stage.removeChild(this.loadingView);
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            this.createGameScene();
        }
    }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onItemLoadError(event:RES.ResourceEvent):void {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onResourceLoadError(event:RES.ResourceEvent):void {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //Ignore the loading failed projects
        this.onResourceLoadComplete(event);
    }

    /**
     * preload资源组加载进度
     * Loading process of preload resource group
     */
    private onResourceProgress(event:RES.ResourceEvent):void {
        if (event.groupName == "preload") {      
            if(this.loadingView.blood){
                this.loadingView.blood.scaleX = 13.34 * (event.itemsLoaded / event.itemsTotal);
            }
        }
    }

    /**
     * 创建游戏场景
     * Create a game scene
     */
    private createGameScene():void {
        this.stageW= this.stage.stageWidth;
        this.stageH = this.stage.stageHeight;

        let jk_bg:egret.Bitmap = this.createBitmapByName("bg_jpg");
        jk_bg.width = this.stageW;
        jk_bg.height = this.stageH;
        this.addChild(jk_bg);


        // 获取摆放格子的横纵坐标
        let hor = Math.floor(this.stageW/100);
        let row = Math.floor((this.stageH-520)/100);
        this.v_coordinate = row;
        // egret.log(hor , row);
  
        //创建物体摆放的格子
        for(let i = 0;i < hor; i++){
            this.coordinate.push([])
            for(let j = 0; j< row; j++){
                this.coordinate[i][j] = {
                    'state' : 0,
                    'x' : i * 100 + Math.random()*10,
                    'y' : j * 100 + Math.random()*20+550
                };
           }
        }


         //创建金子、石头等元素
        this.isCreate( 1 , "big_gold_png",90)
        this.isCreate(0 , "gold_png",60)
        this.isCreate(0 , "gold_1_png",60)
        this.isCreate(0 , "gold_2_png",60)
        this.isCreate( 2 , "big_stone_png",90)
        this.isCreate(0 , "stone_png",60);
        this.isCreate(0 , "stone_1_png",60);
        this.isCreate(0 , "A_png", 80);
        this.isCreate(0 , "B_png", 80);


         //矿工
        this.miner = new egret.Bitmap();
        this.miner.texture = RES.getRes("miner_1_png");
        this.miner.width = 150;
        this.miner.height = 200;
        this.miner.x = this.stageW/2-75;
        this.miner.y = 255;
        this.addChild(this.miner);

            
        //钩子
        this.line = new Arrow("arr_1_png" , "arrLine_png");
        this.line.width = 100;
        this.line.height = 90;
        this.line.x = this.stageW/2;
        this.line.y = 453;
        this.line.anchorOffsetX = 50;
        this.line.anchorOffsetY = 0;
        this.line.rotation = 70;
        this.addChild(this.line); 
        

        // 添加分数
        this.score = new Score();
        this.score.x = this.stageW / 4 * 2.3;
        this.score.y = 26;
        this.addChild(this.score);
        
        // 机会
        this.chances = new Chances();
        this.addChild(this.chances);

        // 没有机会 提示
        this.nochance = new NoChancesTips();
        this.nochance.width = this.stageW*0.95;
        this.nochance.x = this.stageW*0.025;
        this.nochance.y = 300;

        // 沙漏、时间
        this.TimeClass = new Time();
        this.addChild(this.TimeClass);

        // 分享小提示预加载
        this.shareTips = new Share();

        //游戏结束预加载
        this.gameOver = new Gameover();
        this.gameOver.width = this.stageW*0.95;
        this.gameOver.x = this.stageW*0.025;
        this.gameOver.y = 325;

        // 加载失败 预加载
        this.failload = new FailLoad();
        this.failload.width = this.stageW*0.95;
        this.failload.x = this.stageW*0.025;
        this.failload.y = 325;
       
         //开始倒数前，倒数3秒
        this.gameStartTime = new Gamestart();  

        // new request
        this.request = new Request((request) => {
            let response = JSON.parse(request.response);
            // 判断chance机会是否等于0
            let times = response.data.chance.times;
            if( times == 0 ){
                //没有机会
                this.addChild(this.nochance);
                this.chances.times.text = times;
                console.log(this.nochance);
                console.log(this.nochance.backButton);
                console.log(this.nochance.shareButton);
                //返回专题
                this.nochance.backButton.touchEnabled = true;
                this.nochance.backButton.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
                    window.location.href=" "   //专题链接
                } , this)

                //查看好友排名
                this.nochance.checkButton.touchEnabled = true;
                this.nochance.checkButton.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
                    // 排名情况

                } , this)

                //一键分享
                this.nochance.shareButton.touchEnabled = true;
                this.nochance.shareButton.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
                    this.addChild(this.shareTips);
                } , this);
                this.shareTips.touchEnabled = true;
                this.shareTips.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
                    this.removeChild(this.shareTips);
                } , this);

                return;
            }else{
                this.chances.times.text = times;
                // 规则tips
                this.tipsBG = new Reqular();
                this.tipsBG.width = this.stageW*0.96;
                this.tipsBG.height = this.tipsBG.width * (864/1185);
                this.tipsBG.x = 15;
                this.tipsBG.y = 320;
                this.addChild(this.tipsBG);

                // 允许点击
                this.tipsBG.button.touchEnabled = true;
                this.tipsBG.button.addEventListener(egret.TouchEvent.TOUCH_TAP , () =>{
                    // 删除规则
                    this.removeChild(this.tipsBG);

                    //添加3s
                    this.addChild(this.gameStartTime);

                    //3s倒计时：
                    let starttimer = new egret.Timer(1000,3);
                    //注册事件侦听器
                    starttimer.addEventListener(egret.TimerEvent.TIMER,this.gametimeStart,this);
                    starttimer.addEventListener(egret.TimerEvent.TIMER,this.gametimeover,this);
                    //开始计时
                    starttimer.start();

                } , this);   
            }          
        });   
        this.request.sendGetRequest();                              
    }

    // 3s倒数开始
    private gametimeStart():void{
       this.startTime-=1;
       this.gameStartTime.gameTime.text =  this.startTime.toString();
    }

    // 3s倒数结束
    private gametimeover():void{
        if(this.startTime <= 0){
            this.removeChild(this.gameStartTime);
            egret.Tween.get(this.line,{
                loop : true,//循环
            }).to({
                rotation:-70
            },2000).to({
                rotation : 70
            },2000)   

            //倒计时：
            let timer:egret.Timer = new egret.Timer(1000,30);
            //注册事件侦听器
            timer.addEventListener(egret.TimerEvent.TIMER,this.timerFunc,this);
            timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this);
            //开始计时
            timer.start();

            // 允许点击
            this.touchEnabled = true;
            this.addEventListener(egret.TouchEvent.TOUCH_TAP , this.startMove , this);
        }
        
    }

    //30s游戏开始计时
    private timerFunc(){
        this.time-= 1;   
        if(this.time <= 0){
            this.time = 0;
        }
        // console.log(this.time.toString())
        if(this.time <= 9){
            this.TimeClass.unit.text = "0" + this.time.toString();
        }else{
            this.TimeClass.unit.text = this.time.toString();
        }
    }

    // 30s游戏时间结束
    private timerComFunc(){
        egret.Tween.removeTweens(this.line);
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP , this.startMove , this);
        this.removeEventListener(egret.Event.ENTER_FRAME , this.goldMining , this);
        this.removeEventListener(egret.Event.ENTER_FRAME , this.goBack , this);

        //回到原始状态
        this.line.arrowLine.height = 20;
        this.line.arrowGold.y = 70;
        this.line.height = 90;

        this.gameStop();
    }


    //钩子开始下拉
    private startMove(){
        this.addEventListener(egret.Event.ENTER_FRAME , this.goldMining , this);
    }

    // 钩子下拉运动
    private goldMining(){
        egret.Tween.pauseTweens(this.line);
       //中间值midAngle
        let midAngle = (180 / Math.PI) * Math.atan(this.stageW / (2 * ( this.stageH - 543 ) ) )  ;

       //矿工改变状态，  --按下状态
        this.miner.texture = RES.getRes("miner_2_png");
        
        // 弧度绝对值
        let rotation = Math.abs(this.line.rotation); 
        let r = Math.abs(this.line.rotation) * Math.PI/180;
        let len:number;
        
        if(rotation === 0){
            len = this.stageH - 543;
        } else if(rotation <= midAngle){
            len = (this.stageH - 543) /Math.cos(r);
        } else if(rotation > midAngle){
            len = this.stageW / (2 * Math.sin(r));
        }

        let fps: number = len / 200;
        let speedOffset: number = 60 / fps;

        this.line.arrowLine.height += 20;
        this.line.arrowGold.y += 20;
        this.line.height += 20;
        
        //钩子的xy值
        let x = this.line.rotation > 0 ? this.stageW / 2 - (this.line.arrowGold.y + 70) * Math.sin(r) : (this.line.arrowGold.y + 70) * Math.sin(r) + this.stageW / 2 ;
        let y = this.line.arrowGold.y * Math.cos(r) + 453 ;
        this.s = this.line.arrowGold.y;
        
        let tmpi:number = 0;
        //let tmpArr: Array<egret.Bitmap> = [];
        for(tmpi;tmpi < this.tmp.length; tmpi++){
            // console.log(this.hitTest(this.tmp[tmpi] , this.line.arrowGold));
            // 碰撞目标
            const target = this.tmp[tmpi];
            // 碰撞测试
            if(target.hitTestPoint(x,y,true) || target.hitTestPoint(x-5,y-5,true) || target.hitTestPoint(x+5,y+5,true)){
                // 设置金子或石头的深度
                this.setChildIndex(target , (this.tmp.length-1));
                // 打开钩子
                this.line.arrowGold.texture = RES.getRes("arr_2_png");
                // 分数
                let s = new Score().eachScore(tmpi)[0];
                let spe = new Score().eachScore(tmpi)[1];
                // 
                let judgeX = (x + 20) - (target.x+target.width/2); 
                target.x = x - target.width/2;
                // target.x = judgeX > 0 ?  target.x + judgeX : target.x - judgeX;
                // target.y = y - target.height/4;
                target.y = y + 15;
                this.removeEventListener(egret.Event.ENTER_FRAME,this.goldMining,this);
                // console.log(tmpi, '===',(function(o){console.log(o);return o;})(tmpi));
                if(this.time > 0){
                    this.addEventListener(egret.Event.ENTER_FRAME,(function(o,context){
                        var a = function(){
                            context.goBack(s,spe,o,a,context.line.rotation);                       
                        }
                        return a;
                    })(tmpi,this) , this)
                    this._timer = setInterval( () => {
                        if(this.line.arrowLine.height >= 30){
                            // 矿工状态改变                    
                                this.minerChange()
                        }
                    },400)
                }
                
            }
        }
        
        //如果没有碰到，即返回
        if (this.line.arrowGold.y >= len - 20) {
            this.line.arrowGold.texture = RES.getRes("arr_3_png");
            this.removeEventListener(egret.Event.ENTER_FRAME , this.goldMining , this);
            this.addEventListener(egret.Event.ENTER_FRAME , this.getback , this);            
        }
             
    }
    
    //没有碰撞到物体时返回
    private getback():void{
        // 禁止多次点击
        this.touchEnabled = false;

        this.line.arrowLine.height -=20;
        this.line.arrowGold.y -=20;
        this.line.height -=20;
        if(this.line.arrowLine.height <= 30){
            // 允许点击
            this.touchEnabled = true;

             //矿工改变状态，  --原本状态
            this.miner.texture = RES.getRes("miner_1_png");
            //钩子回到原本状态
            this.line.arrowGold.texture = RES.getRes("arr_1_png");
            // 右侧显示表情
            let emotion = this.score.meme;
            emotion.x = this.stageW/2 + 70;
            emotion.text = "n";
            this.addChild(emotion);
            let removerEmotion = setTimeout( () => {
                this.removeChild(emotion);
            } , 700)
            this.removeEventListener(egret.Event.ENTER_FRAME , this.getback , this);
            if(this.time >= 0){
                egret.Tween.resumeTweens(this.line);
            }
        }
    }
    
    //碰撞了物体返回
    private goBack(score:number , speed:number , i:number , context , rotation){      

        // 禁止再次点击
        this.touchEnabled = false;
        
        let speedTime = (this.s) / speed;
        this.line.arrowLine.height -= speed;
        this.line.arrowGold.y -= speed;
        this.line.height -= speed;
        let r = rotation * Math.PI/180;
        this.tmp[i].y -= speed * Math.cos(Math.abs(r));
        this.tmp[i].x -= speed * -Math.sin(r);

        if(this.line.arrowLine.height <= 30){
            
             // 允许点击
            this.touchEnabled = true;

            clearInterval(this._timer);
            //矿工改变状态，  --原本状态
            this.miner.texture = RES.getRes("miner_1_png");
            // console.log('goback')
            this.removeEventListener(egret.Event.ENTER_FRAME , this.getback , this);
            this.removeEventListener(egret.Event.ENTER_FRAME , context , this);
            if(this.time > 0){
                let restTime = parseInt(this.TimeClass.unit.text);
                let that = this;

                //头顶飘分数
                let perScore = this.score.pershowScore;
                perScore.x = this.stageW/2 - 50;
                if(score > 0){
                    perScore.text = '+' + score.toString();
                }else{
                    perScore.text = score.toString();
                }
                
                this.addChild(perScore);

                // 右侧显示表情
                let emotion = this.score.meme;
                emotion.x = this.stageW/2 + 90;
                if(score > 5 ){
                    emotion.text = "h";
                }else{
                    emotion.text = "n";
                }
                this.addChild(emotion);

                egret.Tween.get(perScore).to({
                    y : 150
                },800).call( () =>{
                    this.score.pershowScore.y = 300;
                });
                let setTime = setTimeout(() => {
                    this.removeChild(perScore);
                    this.removeChild(emotion);
                    // this.removeChild(this.score.pershowScore);
                    if( restTime >= 0){
                        egret.Tween.resumeTweens(this.line);
                        egret.Tween.get(this.tmp[i]).to({
                            alpha:0
                        },200).wait(100).to({
                            x : -this.stageW
                        })             
                        //关闭钩子
                        this.line.arrowGold.texture = RES.getRes("arr_1_png");       
                        // console.log(this.score.totalScore.text)
                        if(this.time > 0){
                            // 每次抓到元素，提交分数
                            let params = "score="+score;

                            let request = new Request((request) => {
                                let response = JSON.parse(request.response);
                                let isEgal = response.data["perScore"].isrange;
                                console.log(isEgal);
                                if(isEgal){
                                    console.log(isEgal);
                                    if((parseInt(this.score.totalScore.text)+score) <= 0){
                                        this.score.totalScore.text = '0'
                                    }else{
                                        this.score.totalScore.text = (parseInt(this.score.totalScore.text)+score).toString();
                                    }                              
                                }
                            });   
                            request.sendPostRequest(params);                             
                        }                      
                    }
                }, 500); 
            }         
        }
    }
   

    // 创建金子等元素元素
    private isCreate(maxNum:number , name:string , midW:number):void{
        let i:number = 0;
            // maxNum = 1,
            // midW = 110;
        for(;i <= maxNum;i++){
            let chance = Math.random();
            let num:number = Math.random()*90+20;           
            let stoneName = this.createBitmapByName(name);
            stoneName.width = stoneName.height =midW + Math.random()*20;      
            let i:number = 0;
            let count:number = 0;
            let l = Math.floor(Math.random()*5);
            let r = Math.ceil(Math.random()*4);
            if(this.coordinate[l][r].state){
                l = Math.ceil(Math.random()*5);
                r = Math.ceil(Math.random()*4);
                if(this.coordinate[l][r].state){                  
                    l = Math.ceil(Math.random()*5);
                    r = Math.ceil(Math.random()*4);  
                    let k_len = this.coordinate.length;      
                    console.log(k_len);              
                    if(this.coordinate[l][r].state){
                        for(let k = (this.coordinate.length -1); k > 0; k--){
                            for(let j = (this.v_coordinate - 2); j > 0;j--){
                                let gold = this.coordinate[k][j];
                                if(!gold.state){
                                    l = k;
                                    r = j;
                                }
                            }
                        }    
                    }                                                                                           
                }
            }
            this.coordinate[l][r].state = 1;         
            stoneName.x = this.coordinate[l][r].x;
            stoneName.y = this.coordinate[l][r].y;                  
            this.tmp.push(stoneName);  
            this.addChild(stoneName);         
        }
    }


    //矿工改变状态，  --按下状态   
    private minerChange(){          
        if(this.index == 2){
            this.index +=1;
            this.miner.texture = RES.getRes("miner_2_png");
            
        }else if(this.index == 3){
            this.index -=1;
            this.miner.texture = RES.getRes("miner_3_png");
        }
    }

    private gameStop():void{
        
        //背景画卷
        this.gameOver.gallery.width = this.stageW*0.96;
        this.gameOver.gallery.height = this.gameOver.gallery.width * (864/1185);
        this.gameOver.gallery.x = 8;

        //总分提交
        let params = "score=" + this.score.totalScore.text;
        let request = new Request((request) => {
            let response = JSON.parse(request.response);
            let quan = response.data.quan;
            let totalisEgal = response.data.totalScore.isrange;
            if(totalisEgal){
                let totscore = parseInt(this.score.totalScore.text);
                if(parseInt(this.score.totalScore.text) >= 15){
                    //通过标题：成功or失败
                    this.gameOver.overTit.x = 86; 
                    this.gameOver.overTit.text = '成' + this.score.totalScore.text + '分';

                    //通关详情
                    this.gameOver.overInfo.x = 110;

                    // 三个按钮
                    this.gameOver.first.text = '立';
                    this.gameOver.second.text = '继';
                    this.gameOver.third.text = '一';

                    if( totscore >= 15 && totscore<= 24){
                        //对应券值
                        let quanValue = quan.level1;
                        console.log(quanValue , quan.level1);
                        this.gameOver.overInfo.text = '恭'+quan.level1+'券\n查';
                    }else if( totscore >= 25 && totscore<= 34){
                        //对应券值
                        this.gameOver.overInfo.text = '恭'+quan.level2+'券\n查';
                    }else if( totscore >= 35 && totscore< 84){
                        //对应券值
                        this.gameOver.overInfo.text = '恭'+quan.level3+'券\n查';
                    }else if(totscore >= 85){
                        //对应券值
                        this.gameOver.overInfo.text = '恭'+quan.level4+'券\n查';
                    }                   
                    
                }else{
                    //通过标题：成功or失败
                    this.gameOver.overTit.x = 200; 
                    this.gameOver.overTit.text = '败';

                    //详情
                    this.gameOver.overInfo.x = 160;
                    this.gameOver.overInfo.text = '生';

                    // 三个按钮
                    this.gameOver.first.text = '化';
                    this.gameOver.second.text = '不';
                    this.gameOver.third.text = '一';
                }
            }
        });   
        request.sendPostRequest(params);  //sendPostRequest
        this.addChild(this.gameOver);

        //立即使用优惠券 or 化悲愤为购物欲
        this.gameOver.first.touchEnabled = true;
        this.gameOver.first.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
            window.location.href=" "; 
        } , this)

        //继续游戏 or 不服来战
        this.gameOver.second.touchEnabled = true;
        this.gameOver.second.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
            window.location.href=window.location.href+"?id="+Math.random(); 
        } , this)

        //一键分享
        this.gameOver.third.touchEnabled = true;
        this.gameOver.third.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
            this.addChild(this.shareTips);
        } , this);
        this.shareTips.touchEnabled = true;
        this.shareTips.addEventListener(egret.TouchEvent.TOUCH_TAP , () => {
            this.removeChild(this.shareTips);
        } , this);

    }


    //碰撞检测
    public hitTest(obj1: egret.DisplayObject,obj2: egret.DisplayObject):boolean{
        let rect1:egret.Rectangle = obj1.getBounds();//获取显示对象的测量边界
        let rect2:egret.Rectangle = obj2.getBounds();
        rect1.x = obj1.x;
        rect1.y = obj1.y;
        rect2.x = obj2.x;
        rect2.y = obj2.y;
        //此方法检查指定的 Rectangle 对象的 x、y、width 和 height 属性，以查看它是否与此 Rectangle 对象相交。
        return rect1.intersects(rect2);
    }

    /**
     * 根据imgName关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    private createBitmapByName(name:string):egret.Bitmap {
        let result = new egret.Bitmap();
        let texture:egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }

}


